package logic

import (
	"context"
	"duoduo/duoduorpc/duoduorpc"
	"duoduo/duoduorpc/internal/svc"
	"duoduo/model/mysql"
	"duoduo/pkg"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *duoduorpc.LoginRequest) (*duoduorpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var m mysql.User
	mysql.Db.Debug().Where("account=?", in.Account).Find(&m)
	if m.ID == 0 {
		return nil, fmt.Errorf("已存在,请前去注册")
	}
	if m.Password != pkg.Md5(in.Password) {
		return nil, fmt.Errorf("账号密码输入错误")
	}
	mysql.Db.Debug().Find(&m)
	return &duoduorpc.LoginResponse{
		UserId: int64(m.ID),
	}, nil
}
